/**************************************************************************************************
  Filename:       zcl_poll_control.h
  Revised:        $Date: 2014-01-15 11:31:23 -0800 (Wed, 15 Jan 2014) $
  Revision:       $Revision: 36854 $

  Description:    This file contains the ZCL Poll Control definitions.


  Copyright 2013 - 2014 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/

#ifndef ZCL_POLL_CONTROL_H
#define ZCL_POLL_CONTROL_H

#ifdef __cplusplus
extern "C"
{
#endif

/******************************************************************************
 * INCLUDES
 */
#include "zcl.h"


/******************************************************************************
 * CONSTANTS
 */

/*****************************************/
/***  Poll Control Cluster Attributes ***/
/*****************************************/

  // Server Attributes
#define ATTRID_POLL_CONTROL_CHECK_IN_INTERVAL               0x0000   // M, RW, UINT32
#define ATTRID_POLL_CONTROL_LONG_POLL_INTERVAL              0x0001   // M, R,  UINT32
#define ATTRID_POLL_CONTROL_SHORT_POLL_INTERVAL             0x0002   // M, R,  UINT16
#define ATTRID_POLL_CONTROL_FAST_POLL_TIMEOUT               0x0003   // M, RW, UINT16
#define ATTRID_POLL_CONTROL_CHECK_IN_INTERVAL_MIN           0x0004   // O, R,  UINT32
#define ATTRID_POLL_CONTROL_LONG_POLL_INTERVAL_MIN          0x0005   // O, R,  UINT32
#define ATTRID_POLL_CONTROL_FAST_POLL_TIMEOUT_MAX           0x0006   // O, R,  UINT16


  // Server Attribute Defaults
#define ATTR_DEFAULT_POLL_CONTROL_CHECK_IN_INTERVAL         0x00003840    //  1 hour, in quarterseconds
#define ATTR_DEFAULT_POLL_CONTROL_LONG_POLL_INTERVAL        0x00000014    //  5 seconds, in quarterseconds
#define ATTR_DEFAULT_POLL_CONTROL_SHORT_POLL_INTERVAL       0x0002        //  0.5 seconds, in quarter seconds
#define ATTR_DEFAULT_POLL_CONTROL_FAST_POLL_TIMEOUT         0x0028        // 10 seconds, in quarter seconds
#define ATTR_DEFAULT_POLL_CONTROL_CHECK_IN_INTERVAL_MIN     0x00000028    // 10 seconds, in quarter seconds
#define ATTR_DEFAULT_POLL_CONTROL_LONG_POLL_INTERVAL_MIN    0x0000000c    //  3 seconds, in quarter seconds
#define ATTR_DEFAULT_POLL_CONTROL_FAST_POLL_TIMEOUT_MAX     0x00f0        // 60 seconds, in quarter seconds

#define POLL_CONTROL_CHECK_IN_ABS_MAX                       0x006e0000    // 20 days maximum

  // commands generated by server
#define COMMAND_POLL_CONTROL_CHECK_IN                                    0x00     // M, no payload

  // commands generated by client
#define COMMAND_POLL_CONTROL_CHECK_IN_RSP                                0x00     // M, zclCmdPollControlCheckInRspPayload_t
#define COMMAND_POLL_CONTROL_FAST_POLL_STOP                              0x01     // M, no payload
#define COMMAND_POLL_CONTROL_SET_LONG_POLL_INTERVAL                      0x02     // O, zclCmdSetLongPollIntervalPayload_t
#define COMMAND_POLL_CONTROL_SET_SHORT_POLL_INTERVAL                     0x03     // O, zclCmdSetShortPollIntervalPayload_t



/*******************************************************************************
 * TYPEDEFS
 */

/*** ZCL Poll Control Cluster: Check In Response payload ***/
typedef struct
{
  bool   startFastPolling;
  uint16 fastPollTimeout;
} zclCmdPollControlCheckInRspPayload_t;

/*** ZCL Poll Control Cluster: Set Long Poll Interval payload ***/
typedef struct
{
  uint32 newLongPollInterval;
} zclCmdSetLongPollIntervalPayload_t;

/*** ZCL Poll Control Cluster: Set Short Poll Interval payload ***/
typedef struct
{
  uint16 newShortPollInterval;
} zclCmdSetShortPollIntervalPayload_t;


/*** Structures used for callback functions ***/
typedef struct
{
  afAddrType_t *srcAddr;
  uint8         seqNum;
} zclPollControlCheckIn_t;

typedef struct
{
  uint8     startFastPolling;
  uint16    fastPollTimeOut;
} zclPollControlCheckInRsp_t;

typedef struct
{
  uint32    newLongPollInterval;
} zclPollControlSetLongPollInterval_t;

typedef struct
{
  uint16    newShortPollInterval;
} zclPollControlSetShortPollInterval_t;


// This callback is called to process a CheckIn command on a client (ZR), who may configure the server (ZED)
// srcAddr - address/endpoint of ZED that is checking in.
typedef ZStatus_t (*zclPoll_Control_CheckIn_t)( zclPollControlCheckIn_t *pCmd );
typedef ZStatus_t (*zclPoll_Control_CheckInRsp_t)( zclPollControlCheckInRsp_t *pCmd );
typedef ZStatus_t (*zclPoll_Control_FastPollStop_t)( void );
typedef ZStatus_t (*zclPoll_Control_SetLongPollInterval_t)( zclPollControlSetLongPollInterval_t *pCmd );
typedef ZStatus_t (*zclPoll_Control_SetShortPollInterval_t)( zclPollControlSetShortPollInterval_t *pCmd );


// Register Callbacks table entry - enter function pointers for callbacks that
// the application would like to receive
typedef struct
{
  zclPoll_Control_CheckIn_t                           pfnPollControl_CheckIn;
  zclPoll_Control_CheckInRsp_t                        pfnPollControl_CheckInRsp;
  zclPoll_Control_FastPollStop_t                      pfnPollControl_FastPollStop;
  zclPoll_Control_SetLongPollInterval_t               pfnPollControl_SetLongPollInterval;
  zclPoll_Control_SetShortPollInterval_t              pfnPollControl_SetShortPollInterval;
} zclPollControl_AppCallbacks_t;


/******************************************************************************
 * FUNCTION MACROS
 */

/******************************************************************************
 * VARIABLES
 */

/******************************************************************************
 * FUNCTIONS
 */

/*
 * Register for callbacks from this cluster library
 */
extern ZStatus_t zclPollControl_RegisterCmdCallbacks( uint8 endpoint, zclPollControl_AppCallbacks_t *callbacks );


/*
 * @fn      zclPollControl_Send_CheckIn
 *
 * @brief   Call to send out Poll Control CheckIn command from ZED to ZR/ZC. The Rsp
 *          will indicate whether to stay awake or go back to sleep.
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
extern ZStatus_t zclPollControl_Send_CheckIn( uint8 srcEP, afAddrType_t *dstAddr,
                                              uint8 disableDefaultRsp, uint8 seqNum );

/*********************************************************************
 * @fn      zclPollControl_Send_CheckInRsp
 *
 * @brief   Call to send out Poll Control CheckInRsp. This will tell the ZED
 *          who just checked in to stay awake or to sleep.
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   startFastPolling - tell ZED to stay awake and poll at fast rate
 * @param   fastPollTimeout - number of 1/4 seconds for ZED to stay awake and fast poll
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
extern ZStatus_t zclPollControl_Send_CheckInRsp( uint8 srcEP, afAddrType_t *dstAddr,
                                                 uint8 startFastPolling, uint16 fastPollTimeout,
                                                 uint8 disableDefaultRsp, uint8 seqNum );

/*********************************************************************
 * @fn      zclPollControl_Send_FastPollStop
 *
 * @brief   Call to send out Poll Control FastPollStop. This will tell the ZED
 *          to stop fast polling and go back to sleep at the long poll rate.
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
ZStatus_t zclPollControl_Send_FastPollStop( uint8 srcEP, afAddrType_t *dstAddr,
                                            uint8 disableDefaultRsp, uint8 seqNum );

/*********************************************************************
 * @fn      zclPollControl_Send_SetLongPollInterval
 *
 * @brief   Call to send out a Poll Control Set Long Poll Interval command
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   newLongPollInterval - new long poll interval in 1/4 seconds
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
ZStatus_t zclPollControl_Send_SetLongPollInterval( uint8 srcEP, afAddrType_t *dstAddr,
                                                   uint32 newLongPollInterval,
                                                   uint8 disableDefaultRsp, uint8 seqNum );

/*********************************************************************
 * @fn      zclPollControl_Send_SetShortPollInterval
 *
 * @brief   Call to send out a Poll Control Set Long Poll Interval command
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   newShortPollInterval - new short poll interval in 1/4 seconds
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
ZStatus_t zclPollControl_Send_SetShortPollInterval( uint8 srcEP, afAddrType_t *dstAddr,
                                                    uint16 newShortPollInterval,
                                                    uint8 disableDefaultRsp, uint8 seqNum );

/*********************************************************************
*********************************************************************/

#ifdef __cplusplus
}
#endif

#endif /* ZCL_POLL_CONTROL_H */

